package com.chazaqdev.meatbagsorter.gwt.client;

import com.chazaqdev.meatbagsorter.gwt.client.PhotoServiceAsync;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Image;

public class PhotoPresenter extends Composite {
	private FlowPanel pnlImages;
	private PhotoServiceAsync photoService;
	Image imgMain;
	TextBox txtSearch;
	private long[] photoIds;
	private String[] photoPaths;
	private Image[] imageList;

	private static final String CONTEXT_PATH = 
        GWT.getModuleBaseURL().replace(GWT.getModuleName() + "/", "");

	private static final String CONTEXT_PATH_ROOT = CONTEXT_PATH.substring(0, CONTEXT_PATH.length() -5);

	private void viewImage(int imageID) {
		imgMain.setUrl(CONTEXT_PATH_ROOT + photoPaths[imageID] + "/" + photoIds[imageID] + "_600.jpeg");
	}
	
	private void setupImages() {
		for (int i = 0; i < photoIds.length; ++i) {
			Image image = new Image(CONTEXT_PATH_ROOT + photoPaths[i] + "/" + photoIds[i] + "_100.jpeg");
			final int imageID = i;
			image.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					viewImage(imageID);
				}
			});
			pnlImages.add(image);
		}
		if (photoIds.length > 0) {
			viewImage(0);
		}
	}
	
	private void searchForParticipantsPhotos(String number) {
		photoService.getParticipantsPhotos(number, new AsyncCallback() {
			@Override
			public void onFailure(Throwable caught) {}
			
			@Override
			public void onSuccess(Object jsonResponse) {
				pnlImages.clear();
				imgMain.setUrl("./");
				txtSearch.setText("");
				JSONArray jsonArray;
            	JSONValue value = JSONParser.parse((String)jsonResponse);
				JSONNumber number;
				JSONString string;

            	if ((jsonArray = value.isArray()) != null) {
            		photoIds = new long[jsonArray.size()/2];
            		photoPaths = new String[jsonArray.size()/2];
            		for (int i = 0; i < jsonArray.size()/2; ++i) {
            			value = jsonArray.get(i*2);
            			if ((string = value.isString()) != null) {
            				photoIds[i] = Long.valueOf(string.stringValue());
            			}
            			value = jsonArray.get(i*2+1);
            			if ((string = value.isString()) != null) {
            				photoPaths[i] = string.stringValue();
            			}
            		}
            	}
            	setupImages();
			}
			
		});
	}
	
	public PhotoPresenter() {
    	photoService = (PhotoServiceAsync) GWT.create(PhotoService.class);
    	ServiceDefTarget endpoint = (ServiceDefTarget) photoService;
		String moduleRelativeURL = GWT.getModuleBaseURL() + "rpc"; 
		endpoint.setServiceEntryPoint(moduleRelativeURL);
		
		VerticalPanel verticalPanel = new VerticalPanel();
		initWidget(verticalPanel);
		
		FlowPanel flowPanel = new FlowPanel();
		verticalPanel.add(flowPanel);
		
		txtSearch = new TextBox();
		flowPanel.add(txtSearch);
		txtSearch.setWidth("274px");
		
		Button btnSearch = new Button("Search");
		flowPanel.add(btnSearch);
		
		FlowPanel flowPanel_1 = new FlowPanel();
		verticalPanel.add(flowPanel_1);
		
		imgMain = new Image((String) null);
		flowPanel_1.add(imgMain);
		imgMain.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				String s = imgMain.getUrl();
				s = s.replace("_600.jpeg", "_orig.jpeg");
				Window.open(s, "", "");
			}
		});
		
		ScrollPanel scrollPanel = new ScrollPanel();
		verticalPanel.add(scrollPanel);
		
		
		pnlImages = new FlowPanel();
		scrollPanel.setWidget(pnlImages);
		pnlImages.setSize("100%", "100%");
		
//		Image image_1 = new Image((String) null);
//		pnlImages.add(image_1);
//		
//		Image image_2 = new Image((String) null);
//		pnlImages.add(image_2);
		
		txtSearch.addKeyUpHandler(new KeyUpHandler() {
			
			@Override
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					searchForParticipantsPhotos(txtSearch.getText());
					
				}
			}
		});
		
		btnSearch.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				searchForParticipantsPhotos(txtSearch.getText());
			}
		});
	}

}
